package com.thkj.business.cookbook.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.thkj.business.cookbook.entity.TblFoodProp;
import com.thkj.business.cookbook.service.TblFoodPropService;
import com.thkj.util.RequestUtil;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import dp.R;
import dp.dao.PagingList;
import dp.util.DateUtil;
import com.thkj.framework.base.BaseController;

@RestController
@RequestMapping("/backstage/foodProp")
public class FoodPropController extends BaseController {

	@Autowired
    TblFoodPropService tblFoodPropService;
    
    @RequestMapping("")
    public ModelAndView index(){
    	return new ModelAndView("foodProp/list");
    }
	
	@RequestMapping("/query")
    public R query(HttpServletRequest request, int page, int size){
    	Map<String, Object> param = RequestUtil.getParameterMap(request);
    	PagingList<Map> list = tblFoodPropService.query(page, size,param);
    	list.getList().forEach(t->{
    		t.put("outline", HtmlUtil.cleanHtmlTag(t.get("content").toString()).replaceAll("&nbsp;",""));
    	});
    	return R.success(list);
    }
	
    @RequestMapping("/edit")
    public R edit(TblFoodProp foodProp){
    	try {
    		if(StrUtil.isBlank(foodProp.getId())) {
    			foodProp.setId(IdUtil.objectId());
    			foodProp.setPublishTime(DateUtil.getNowAsTimestamp());
    			tblFoodPropService.insertTblFoodProp(foodProp);
        	}else {
        		tblFoodPropService.updateTblFoodProp(foodProp);
        	}
        	return R.SUCCESS;
    	}catch (Exception e) {
			return R.failure(e.getMessage());
		}
    }
    
    @RequestMapping("/delete")
    public R delete(String id){
    	tblFoodPropService.deleteTblFoodProp(id);
    	return R.SUCCESS;
    }
}